package com.sumainfor.pc.market.controller;

import com.netflix.discovery.converters.Auto;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.feign.BaseFeignZuul;
import com.sumainfor.pc.market.service.OwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 业主
 * @Auther: zhlu
 * @Date: 2018/11/11 15:56
 * @Description:
 */
@RestController
@RequestMapping("/ow")
public class OwnerController {

    @Autowired
    OwnerService OwnerService;

    @Autowired
    BaseFeignZuul BaseFeignZuul;

    /**
     * 功能描述:
     * 新增待联系业主
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/11 16:00
     */
    @RequestMapping(value = "/AddOwner",method = RequestMethod.POST)
    public JsonResult AddOwner(@RequestBody Map<String,Object> params){
        JsonResult result=new JsonResult();
        if(OwnerService.AddOwner(params)){
            result.putSuccess("添加待联系业主成功！");
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述:
     * 获取待联系业主列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/14 21:43
     */
    @ResponseBody
    @GetMapping("/getOwnerList")
    public JsonResult getOwnerList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
//        params.put("OwState",1);
        params.put("delfg",0);
        Integer cout=OwnerService.getOwnerListCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>> getOwnerList=OwnerService.getOwnerList(params);
        result=pageUtils.getJsonResult(getOwnerList, params, cout);
        return result;
    }

    /**
     * 功能描述:
     * 根据编号查询业主待联系详情
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/15 19:19
     */
    @GetMapping("/getOwnerMap")
    public JsonResult getOwnerMap(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"OwId"})){
            result.putFailed("OwId不能为空！");
            return  result;
        }
        result.put(OwnerService.getOwnerMap(params));
        return result;
    }

    /**
     * 功能描述:
     * 修改业主待联系资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/15 19:55
     */
    @ResponseBody
    @PostMapping("/UpdateOwner")
    public JsonResult UpdateOwner(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"OwId"})){
            result.putFailed("OwId不能为空！");
            return  result;
        }
        if(OwnerService.UpdateOwner(params)){
            result.putSuccess("资料完善成功！");
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述:
     * 根据编号查询列表业主待联系详情
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/16 14:45
     */
    @GetMapping("/getOwnerMaps")
    public JsonResult getOwnerMaps(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"OwId"})){
            result.putFailed("OwId不能为空！");
            return  result;
        }
        Map<String,Object>getOwnerMaps=OwnerService.getOwnerMaps(params);
        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
        if(FieldCheckUtil.isMapFieldNotNull(getOwnerMaps,new String[]{"MiId"})){
            getOwnerMaps= ListMapMergeUtil.MapTranStringUtils(getOwnerMaps,getDeptList,"MiId","DeptId","DeptName","MiName");
        }

        result.put(getOwnerMaps);
        return result;
    }

    /**
     * 功能描述:
     * 根据编号获取盈利预测业主资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/11/19 20:04
     */
    @GetMapping("/getOwnerProMap")
    public JsonResult getOwnerProMap(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"OwId"})){
            result.putFailed("OwId不能为空！");
            return  result;
        }
        Map<String,Object>getOwnerProMap=OwnerService.getOwnerProMap(params);
        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
        if(FieldCheckUtil.isMapFieldNotNull(getOwnerProMap,new String[]{"MiId"})){
            getOwnerProMap= ListMapMergeUtil.MapTranStringUtils(getOwnerProMap,getDeptList,"MiId","DeptId","DeptName","MiName");
        }
        result.put(getOwnerProMap);
        return result;
    }
    
    /**
     * 功能描述: 
     * 获取业主即将到期列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/4 14:11
     */
    @ResponseBody
    @RequestMapping(value="/getOwnerExpiringList",method = RequestMethod.GET)
    public JsonResult getOwnerExpiringList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        //收录状态   1. 新房预约待看  2盈利预测草稿箱    3盈利预测待审核  4盈利预测失效 5待收录房源  6.业主已放弃
        params.put("delfg",0);
        params.put("OwState",5);
        Integer cout=OwnerService.getOwnerExpiringCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>> getOwnerExpiringList=OwnerService.getOwnerExpiringList(params);
        result=pageUtils.getJsonResult(getOwnerExpiringList, params, cout);
        return result;
    }

    /**
     * 功能描述: 
     * 根据业主编号、业主当前合同、盈利预测编号获取业主续签资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/16 17:54
     */
    @GetMapping("/getRenewOwnerMap")
    public JsonResult getRenewOwnerMap(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"OwId"})){
            result.putFailed("OwId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"OwDeId"})){
            result.putFailed("OwDeId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"ProId"})){
            result.putFailed("ProId不能为空！");
            return  result;
        }
        params.put("delfg",0);
        Integer getRenewOwnerAllow=OwnerService.getRenewOwnerAllow(params);
        if(getRenewOwnerAllow>0){
            result.putFailed("业主合同周期已有其他合同！");
            return  result;
        }
        Map<String,Object>getRenewOwnerMap=OwnerService.getRenewOwnerMap(params);
        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
        if(FieldCheckUtil.isMapFieldNotNull(getRenewOwnerMap,new String[]{"MiId"})){
            getRenewOwnerMap= ListMapMergeUtil.MapTranStringUtils(getRenewOwnerMap,getDeptList,"MiId","DeptId","DeptName","MiName");
        }
        result.put(getRenewOwnerMap);
        return result;
    }

    /**
     * 功能描述: 
     * 业主续签
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/17 10:12
     */
    @ResponseBody
    @PostMapping("/RenewOwnerAddOwnerDeal")
    public JsonResult RenewOwnerAddOwnerDeal(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(OwnerService.RenewOwnerAddOwnerDeal(params)){
            result.putSuccess("续签保存成功！");
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述: 
     * 收录业主的时候先判断当前业主路号室号是否存在
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/13 13:57
     */
    @PostMapping("/getOwnerPresence")
    public JsonResult getOwnerPresence(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"ComId"})){
            result.putFailed("ComId不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"OwBuilDing"})){
            result.putFailed("OwBuilDing不能为空！");
            return  result;
        }
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"OwRoom"})){
            result.putFailed("OwRoom不能为空！");
            return  result;
        }
        result.put(OwnerService.getOwnerPresence(params));
        return result;
    }
}
